package mil.af.amc.adpetracker.web.actions.User;

import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.adpetracker.web.actions.AbstractAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Action to handle User outprocessing
 *
 * @author TSgt Jason Ferguson
 */

public class OutprocessUserAction extends AbstractAction {

    private Log log = LogFactory.getLog(OutprocessUserAction.class);

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int userId = Integer.parseInt(httpServletRequest.getParameter("userId"));
        try {
            userService.outprocessUser(userId);
        } catch (ServiceException e) {
            log.debug("userService.outprocessUser() threw ServiceException", e);
            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
            
            return actionMapping.getInputForward();
        }

        log.trace("Outprocessed user #" + userId);
        ActionMessage message = new ActionMessage("messages.user.outprocess.success");
        saveError(httpServletRequest, message);

        return actionMapping.findForward("success");


    }
}
